const messageDao = require("@/dao/message");
const z_base_Dao = require("@/dao/z_base");
const MessageModal = require("@/model/Message");
const sequelize = require("@/model/sequelizeBaseConfig");
const { IP } = require("@/utils/ip");
const { ErrorCode } = require("@/utils/r/code");
const { cloneDeep } = require("lodash");
const { QueryTypes } = require("sequelize");
const blogInfoService = require("@/server/blog_info");

const message = {
  async GetList(query) {
    const { total, pageData } = await messageDao.GetList(query);
    return {
      pageSize: +query.page_size,
      pageNum: +query.page_num,
      pageData: pageData,
      total,
    };
  },
  async UpdateReview(body) {
    const maps = { is_review: body.is_review };
    await z_base_Dao.UpdatesMap(MessageModal.Message, maps, { id: body.ids });
    return ErrorCode.OK;
  },
  async Delete(ids) {
    await z_base_Dao.Delete(MessageModal.Message, { id: ids });
    return ErrorCode.OK;
  },
  async GetFrontList() {
    let list = await sequelize.query(
      `SELECT * FROM message WHERE is_review = 1`,
      {
        type: QueryTypes.SELECT,
      }
    );
    return list;
  },
  async Save(req) {
    const ipAddress = IP.getIpAddress(req);
    const ipSource = await IP.getIpSource(ipAddress); //{"data":"美国|0|新泽西|0|0"}
    let message = cloneDeep(req.body);
    message.ip_address = ipAddress;
    message.ip_source = JSON.parse(ipSource).data;
    const { is_message_review } = await blogInfoService.GetBlogConfig();
    message.is_review = is_message_review;
    message.created_at = new Date().toLocaleString();
    message.updated_at = new Date().toLocaleString();
    message.speed = 0; //默认速度
    await z_base_Dao.Create(MessageModal.Message, message);
  },
};

module.exports = message;
